import 'dart:async';
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:wu_you/data/cache/auth_cache.dart';
import 'package:wu_you/data/cache/language_cache.dart';
import 'package:wu_you/http/http.dart';
import 'package:wu_you/main.dart';
import 'package:wu_you/plugins/advertising_plugin.dart';
import 'package:wu_you/plugins/certify_plugin.dart';
import 'package:wu_you/plugins/init_plugin.dart';
import 'package:wu_you/res/colors.dart';
import 'package:wu_you/route/app_routers.dart';
import 'package:wu_you/utils/login_util.dart';
import 'package:wu_you/utils/utils.dart';

class SplashPage extends StatefulWidget{
  @override
  SplashPageState createState() => SplashPageState();
}
class SplashPageState extends State<SplashPage>{
  @override
  void initState() {
    super.initState();
    _onAgree(showAdSplash: true);
  }
  void _onAgree({bool showAdSplash = true}) async {
    await _initApp();
    if(showAdSplash){
      // InitPlugin().showSplashAd();
      _checkToken();
    }else{
      _checkToken();
    }
  }

  ///初始化APP
  Future<void> _initApp() async {
    // InitPlugin().initApp();
    // AdPlugin().init();
  }
  @override
  void dispose() {
    super.dispose();
  }

  void _checkToken (){
    AuthTokenCache().get().then((value){
      Http().token = value;
      LanguageCache().get().then((v){
        Http().language = v;
        if(v == "1"){
          MyApp.setLocale(context, Locale('zh', ''));
        }else{
          MyApp.setLocale(context, Locale('en', ''));
        }
      });
      LoginUtil.initGlobalInfo(context);
      _navigateToMainPage();
    }).catchError((e) {
      _navigateToMainPage();
    });
  }
  void _navigateToMainPage() {
    Navigator.pushNamedAndRemoveUntil(
      context,
      AppRoutes.routeMain,
          (route) => false,
    );
  }

  void _navigateToLoginPage() {
    Navigator.pushNamedAndRemoveUntil(
      context,
      AppRoutes.routeLogin,
          (route) => false,
    );
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: AppColors.whiteColor,
      ),
    );
  }
}